home *** CD-ROM | disk | FTP | other *** search
/ PureBasic (Professional Edition) / NEU.ISO / setup.exe / {app} / Examples / Atomic FTP Server / Atomic FTP Server.pb next >
Text File  |  2002-01-02  |  5KB  |  227 lines

  1. ;
  2. ; ------------------------------------------------------------
  3. ;
  4. ;       Atomic FTP Server in PureBasic by AlphaSND
  5. ;
  6. ;            (c) 2001 - Fantaisie Software
  7. ;
  8. ; ------------------------------------------------------------
  9. ;
  10. ; This program isn't finished, the harder is done but I don't
  11. ; time to implement the whole RFC 959 commands :-).
  12. ;
  13. ; 01/12/2001
  14. ;   Removed all API call by internal PB functions (much easier)
  15. ;
  16. ; 19/03/2001
  17. ;   Listing is now working.
  18. ;
  19. ; 18/03/2001
  20. ;   Based on the Atomic Web Server code..
  21. ;   First version.
  22. ;
  23.  
  24. If InitNetwork() = 0
  25.   MessageRequester("Error", "Can't initialize the network !", 0) : End
  26. EndIf
  27.  
  28. DefType.l
  29.  
  30. ClientIP.s
  31. Port = 21
  32. BaseDirectory$ = "ftp\"
  33. CurrentDirectory$ = BaseDirectory$
  34. AtomicTitle$   = "Atomic FTP Server v0.1"
  35.  
  36. Global EOL$, ClientID
  37.  
  38. EOL$ = Chr(13)+Chr(10)
  39.  
  40. *Buffer = AllocateMemory(0, 10000 , 0)
  41.  
  42. If CreateNetworkServer(Port)
  43.  
  44.   OpenWindow(0, 100, 200, 230, 0, #PB_Window_SystemMenu, "Atomic FTP Server (Port "+Str(Port)+")")
  45.   
  46.   Repeat
  47.     
  48.     WEvent.l = WindowEvent()
  49.     SEvent.l = NetworkServerEvent()
  50.   
  51.     If WEvent = #PB_EventCloseWindow
  52.       Quit = 1
  53.     EndIf
  54.  
  55.     If SEvent
  56.       ClientID.l = NetworkClientID()
  57.   
  58.       Select SEvent
  59.       
  60.         Case 1  ; New client connected
  61.           a$ = "220 - Atomic FTP Server v0.1 ready"+EOL$
  62.           SendNetworkData(ClientID, @a$, Len(a$))
  63.  
  64.         Case 4  ; New client has closed the connection
  65.   
  66.         Default
  67.           RequestLength.l = ReceiveNetworkData(ClientID, *Buffer, 2000)
  68.           If RequestLength > 3
  69.             PokeL(*Buffer+RequestLength-2, 0)
  70.           EndIf
  71.           Gosub ProcessRequest
  72.           
  73.       EndSelect
  74.     Else
  75.       Delay(20)
  76.     EndIf
  77.     
  78.   Until Quit = 1 
  79.   
  80.   CloseNetworkServer()
  81. Else
  82.   MessageRequester("Error", "Can't create the server (port in use ?).", 0)
  83. EndIf
  84.  
  85. End 
  86.  
  87.  
  88. ProcessRequest:
  89.  
  90.   Command$ = PeekS(*Buffer)
  91.   
  92.   Position = FindString(Command$, " ", 1)
  93.   If Position
  94.     Argument$ = Mid(Command$, Position+1, Len(Command$)-Position)
  95.     Command$ = UCase(StripTrail(Left(Command$, Position-1)))
  96.   EndIf
  97.  
  98.   Select Command$
  99.  
  100.     Case "HELP"
  101.       Gosub Command_HELP
  102.  
  103.     Case "LIST"
  104.       Gosub Command_LIST
  105.  
  106.     Case "PASS"
  107.       Gosub Command_PASS
  108.  
  109.     Case "PORT"
  110.       Gosub Command_PORT
  111.  
  112.     Case "PWD"
  113.       Gosub Command_PWD
  114.  
  115.     Case "SYST"
  116.       Gosub Command_SYST
  117.  
  118.     Case "USER"
  119.       Gosub Command_USER
  120.  
  121.     Default
  122.       Gosub Command_UNKNOWN
  123.  
  124.   EndSelect
  125.  
  126. Return
  127.  
  128.  
  129. Command_HELP:
  130.   a$ = "214 - You wanna some help ? :-D"+EOL$
  131.   SendNetworkData(ClientID, @a$, Len(a$))
  132. Return
  133.  
  134.  
  135. Command_LIST:
  136.   a$ = "150 - Opening connection"+EOL$
  137.   SendNetworkData(ClientID, @a$, Len(a$))
  138.  
  139.   If OpenNetworkConnection(ClientIP, ClientPort)
  140.   
  141.     If ExamineDirectory(0, CurrentDirectory$, "*.*")
  142.       a$ = ""
  143.       NumberFiles = 0
  144.       Repeat
  145.         Type = NextDirectoryEntry()
  146.         
  147.         If Type = 1 : a$ = a$+"rwxr-xr-x 6 12545 512 Jan 23 10:18 " +DirectoryEntryName()+EOL$ : EndIf
  148.         If Type = 2 : a$ = a$+"drwxr-xr-x 6 12545 512 Jan 23 10:18 "+DirectoryEntryName()+EOL$ : EndIf
  149.         
  150.         NumberFiles+1
  151.       Until Type = 0
  152.     EndIf
  153.     
  154.     a$ = "total "+Str(NumberFiles)+EOL$+a$
  155.     SendNetworkData(0, @a$, Len(a$))
  156.     CloseNetworkConnection()
  157.   EndIf
  158.  
  159.   a$ = "226 - Listing finished"+EOL$
  160.   SendNetworkData(ClientID, @a$, Len(a$))
  161. Return
  162.  
  163.  
  164. Command_PASS:
  165.   a$ = "230 - Welcome, enjoy this FTP site"+EOL$
  166.   SendNetworkData(ClientID, @a$, Len(a$))
  167. Return
  168.  
  169.  
  170. Command_PORT:
  171.   a$ = "200 - Ok"+EOL$
  172.  
  173.   ; Build a real IP
  174.   ;
  175.   Position = FindString(Argument$, ",", 1) 
  176.   ClientIP.s = ClientIP+Mid(Argument$, 1, Position-1)+"."
  177.  
  178.   NewPosition = FindString(Argument$, ",", Position+1)
  179.   ClientIP = ClientIP+Mid(Argument$, Position+1, NewPosition-Position-1)+"."
  180.  
  181.   Position = FindString(Argument$, ",", NewPosition+1)
  182.   ClientIP = ClientIP+Mid(Argument$, NewPosition+1, Position-NewPosition-1)+"."
  183.  
  184.   NewPosition = FindString(Argument$, ",", Position+1)
  185.   ClientIP = ClientIP+Mid(Argument$, Position+1, NewPosition-Position-1)
  186.  
  187.   ClientIP = StripLead(StripTrail(ClientIP))
  188.  
  189.   ; Get the port..
  190.   ;
  191.   Position = FindString(Argument$, ",", NewPosition+1)
  192.   ClientPort = Val(Mid(Argument$, NewPosition+1, Position-NewPosition-1)) << 8+Val(Right(Argument$, Len(Argument$)-Position))
  193.  
  194.   SendNetworkData(ClientID, @a$, Len(a$))
  195. Return
  196.  
  197.  
  198. Command_PWD:
  199.   a$ = "257 /"+EOL$
  200.   SendNetworkData(ClientID, @a$, Len(a$))
  201. Return
  202.  
  203.  
  204. Command_UNKNOWN:
  205.   a$ = "500 - Unknow command"+EOL$
  206.   SendNetworkData(ClientID, @a$, Len(a$))
  207. Return
  208.  
  209.  
  210. Command_USER:
  211.   If Argument$ = "anonymous"
  212.     a$ = "331 - User anonymous accepted. Please enter your e-mail"+EOL$
  213.   Else
  214.     a$ = "331 - Hello "+Argument$+". Please enter your password"+EOL$
  215.   EndIf
  216.  
  217.   SendNetworkData(ClientID, @a$, Len(a$))
  218. Return
  219.  
  220.  
  221. Command_SYST:
  222.   a$ = "215 - Atomic FTP Server v0.1"+EOL$
  223.   SendNetworkData(ClientID, @a$, Len(a$))
  224. Return
  225. ; ExecutableFormat=Windows
  226. ; Executable=C:\Atomic FTP Server.exe